package besta.moon.sql.expression.function.string;

import besta.moon.sql.parser.SQLTokenizer;
import besta.moon.sql.expression.function.FunctionReturnInt;

public final class FunctionDifference extends FunctionReturnInt {

    @Override
    public final int getFunction() {
        return SQLTokenizer.DIFFERENCE;
    }

    @Override
    public boolean isNull() throws Exception {
        return param1.isNull() || param2.isNull();
    }

    @Override
    public final int getInt() throws Exception {
        if (isNull()) {
            return 0;
        }
        String str1 = FunctionSoundex.getString(param1.getString());
        String str2 = FunctionSoundex.getString(param2.getString());
        int diff = 0;
        for (int i = 0; i < 4; i++) {
            if (str1.charAt(i) == str2.charAt(i)) {
                diff++;
            }
        }
        return diff;
    }
}
